<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    // 浅拷贝
    var extend = function (out) {
        out = out || {};

        for (var i = 1; i < arguments.length; i++) {
            if (!arguments[i])
                continue;

            for (var key in arguments[i]) {
                if (arguments[i].hasOwnProperty(key))
                    out[key] = arguments[i][key];
            }
        }

        return out;
    };

    let newObj = extend({}, { a: 'a', b: 'b' }, { 1: 1, 2: 2 });
    console.log(newObj);

    // 深拷贝
    var deepExtend = function (out) {
        out = out || {};

        for (var i = 1; i < arguments.length; i++) {
            var obj = arguments[i];

            if (!obj) continue;

            for (var key in obj) {
                if (obj.hasOwnProperty(key)) {
                    if (typeof obj[key] === "object" && obj[key] !== null) {
                        if (obj[key] instanceof Array) out[key] = obj[key].slice(0);
                        else out[key] = deepExtend(out[key], obj[key]);
                    } else out[key] = obj[key];
                }
            }
        }

        return out;
    };

    let newObj2 = deepExtend({}, { a: 'a', b: 'b' }, { 1: 1, 2: 2 });
    console.log(newObj2);
</script>

</html>